﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/12/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using CommonLibrary;
using CommonLibrary.Forms;
namespace BusinessLibrary
{
    /// <summary>
    /// MESLogin界面
    /// </summary>
    public partial class FormMESLogin : HWFrmWithTitle
    {
        /// <summary>
        /// 登录方式，0密码1刷卡
        /// </summary>
        private ELoginType LoginType = BusinessConfig.Instance().MESLoginType;
        /// <summary>
        /// 构造函数
        /// </summary>
        public FormMESLogin()
        {
            InitializeComponent();
            this.radioButtonPassword.Checked = LoginType == ELoginType.USERNAME;
            this.radioButtonCard.Checked = LoginType == ELoginType.CARD;
            this.labelLoginImg.Font = new Font(FontHelper.GetFont(), 72);
            this.labelLoginImg.Text = FontHelper.USER;
            LoadLastMESUserName();
        }
        private string mLastMESUserNamePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "last_mes_user");
        private object FILE_LOCK = new object();
        /// <summary>
        /// 最后使用的MES登录用户名
        /// </summary>
        public string LastMESUserName
        {
            get
            {
                lock (FILE_LOCK)
                {
                    string result = string.Empty;
                    if (File.Exists(mLastMESUserNamePath))
                    {
                        result = File.ReadAllText(this.mLastMESUserNamePath, CommonHelper.ENCODING);
                    }
                    return result;
                }
            }
        }
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormOptions_Load(object sender, EventArgs e)
        {
            InitControls();
        }
        /// <summary>
        /// 加载设置内容
        /// </summary>
        private void LoadLastMESUserName()
        {
            this.textBoxUserName.Text = LastMESUserName;
        }
        private void SetLastMESUserName(string userName)
        {
            lock (FILE_LOCK)
            {
                try
                {
                    File.WriteAllText(this.mLastMESUserNamePath, userName, CommonHelper.ENCODING);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                }
            }
        }
        /// <summary>
        /// 用户名框按键触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.textBoxUserName.Text.Trim().Length == 0) return;
            if (e.KeyValue == 13)
            {
                this.textBoxPassword.Focus();
            }
        }
        /// <summary>
        /// 密码框按键触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Password_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.textBoxPassword.Text.Trim().Length == 0) return;
            if (e.KeyValue == 13)
            {
                buttonLogin_Click(null, null);
            }
        }
        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLogin_Click(object sender, EventArgs e)
        {
            bool isValid = true;
            var userName = this.textBoxUserName.Text.Trim();
            var password = this.textBoxPassword.Text.Trim();

            if (string.IsNullOrWhiteSpace(userName))
            {
                isValid = false;
                HWMessageBox.Show(this, "用户名有误");
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                isValid = false;
                HWMessageBox.Show(this, "密码有误");
            }

            if (isValid)
            {
                var mesResult = LoginType == ELoginType.USERNAME ? MesBusiness.LoginPasswordCheck(userName, password) : MesBusiness.LoginCardCheck(userName);

                //模拟，后续需要根据实际情况更改
                if (mesResult.IsSuccessReturn)
                {
                    SetLastMESUserName(userName);
                    MesCommonHelper.MES_USERNAME = userName;
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    if (GlobalStatus.APP != EAppStatus.RUN)
                    {
                        MesCommonHelper.MES_USERNAME = string.Empty;
                    }
                    HWMessageBox.Show(this, $"登录失败：{MesBusiness.GetMessage(mesResult)}");
                }
            }
        }
        private void FormOptions_Shown(object sender, EventArgs e)
        {
        }
        private void FormOptions_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private void radioButtonPassword_CheckedChanged(object sender, EventArgs e)
        {
            InitControls();
        }
        /// <summary>
        /// 初始化各个控件
        /// </summary>
        private void InitControls()
        {
            LoginType = this.radioButtonPassword.Checked ? ELoginType.USERNAME : ELoginType.CARD;
            this.textBoxUserName.Text = LastMESUserName;
            this.textBoxPassword.Text = string.Empty;
            this.textBoxPassword.Enabled = LoginType == ELoginType.USERNAME ? true : false;

            this.textBoxUserName.Focus();
            if (!string.IsNullOrWhiteSpace(this.textBoxUserName.Text.Trim()) && LoginType == ELoginType.USERNAME)
            {
                this.textBoxPassword.Focus();
            }

            BusinessConfig.Instance().MESLoginType = this.radioButtonCard.Checked ? ELoginType.CARD : ELoginType.USERNAME;
            BusinessConfig.Instance().Save();
        }
    }
}
